<template>
  <div>
    <el-upload
      :action="resourcesUrl + '/inf-bridge-service/back/storage/file/upload'"
      :headers="{Authorization: token}"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      multiple
      :on-success="handleUploadSuccess"
      :file-list="imageList"
      :before-upload="beforeAvatarUpload">
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
  /* eslint-disable */
  export default {
    data () {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        resourcesUrl: ''
      }
    },
    props: {
      value: {
        default: '',
        type: String
      },
      sizeVisible: {
        default: false,
        type: Boolean
      },
      limit: {
        default: 10,
        type: Number
      },
    },
    created() {
      this.resourcesUrl = process.env.VUE_APP_BASE_API
      this.token = localStorage.getItem('token')
    },
    computed: {
      imageList () {
        let res = []
        if (this.value) {
          let imageArray = this.value.split(',')
          for (let i = 0; i < imageArray.length; i++) {
            res.push({url: imageArray[i], response: imageArray[i]})
          }
        }
        this.$emit('input', { pics: this.value})
        return res
      }
    },
    methods: {
      // 图片上传
      handleUploadSuccess (response, file, fileList) {
        console.log(fileList)
        let allSucc = fileList.every(item => item.status == 'success')
        if (allSucc) {
          let pics = fileList.map(file => {
            if(file.response && file.response.code == 0) {
              return file.response.data.fileUrl
            } else {
              return file.response
            }
          }).join(',')
          this.$emit('input', { pics })
        }
      },
      // 限制图片上传大小
      beforeAvatarUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < this.limit
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 '+this.limit+'MB!')
          return isLt2M
        }
        if (this.sizeVisible) {
          const isSize = new Promise(function (resolve, reject) {
            let _URL = window.URL || window.webkitURL
            let image = new Image()
            image.onload = function () {
              // console.log(77, image.width, image.height)
              let valid = image.width / image.height === 1
              valid ? resolve() : reject()
            }
            image.src = _URL.createObjectURL(file)
          }).then(
            () => {
              return file
            },
            () => {
              this.$message.error('上传图片尺寸不符合')
              return Promise.reject(false)
            }
          )
          let typeArr=file.name.split('.');
          const fileType = (typeArr[typeArr.length-1]).toLowerCase();
          const fileTypeStr = 'jpg,png';
          if (fileTypeStr.indexOf(fileType) < 0) {
            this.$message.warning('图片格式只能为jpg或png');
            return false;
          }
          return isSize
        }else {
          let typeArr=file.name.split('.');
          const fileType = (typeArr[typeArr.length-1]).toLowerCase();
          const fileTypeStr = 'jpg,png';
          if (fileTypeStr.indexOf(fileType) < 0) {
            this.$message.warning('图片格式只能为jpg或png');
            return false;
          }
        }

        return isLt2M
      },
      handleRemove (file, fileList) {
        let pics = fileList.map(file => {
          if (file.response.data) {
            return file.response.data.fileUrl
          } else {
            return file.response
          }
        }).join(',')
        this.$emit('input', { pics: this.value})
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      }
    }
  }
</script>

<style lang="scss">
</style>